package com.sunday.msq.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sunday.frame.AppManager;
import com.sunday.frame.BaseFragmentActivity;
import com.sunday.frame.LayoutInject;
import com.sunday.frame.ViewInject;
import com.sunday.msq.R;
import com.sunday.msq.fragment.IndexFragment;

/***
 * 
 * @author 晨曦
 *
 */
@LayoutInject(R.layout.home_layout)
public class HomeActivity extends BaseFragmentActivity implements OnClickListener{

	private long ExitTime;
	
	@ViewInject(value=R.id.ll1,onclick=true)
	private LinearLayout ll1;
	@ViewInject(value=R.id.ll2,onclick=true)
	private LinearLayout ll2;
	@ViewInject(value=R.id.ll3,onclick=true)
	private LinearLayout ll3;
	@ViewInject(value=R.id.ll4,onclick=true)
	private LinearLayout ll4;
	@ViewInject(value=R.id.ll5,onclick=true)
	private LinearLayout ll5;
	
	@ViewInject(value=R.id.iv1,onclick=false)
	private ImageView iv1;
	@ViewInject(value=R.id.iv2,onclick=false)
	private ImageView iv2;
	@ViewInject(value=R.id.iv3,onclick=false)
	private ImageView iv3;
	@ViewInject(value=R.id.iv4,onclick=false)
	private ImageView iv4;
	@ViewInject(value=R.id.iv5,onclick=false)
	private ImageView iv5;
	
	@ViewInject(value=R.id.tv1,onclick=false)
	private TextView tv1;
	@ViewInject(value=R.id.tv2,onclick=false)
	private TextView tv2;
	@ViewInject(value=R.id.tv3,onclick=false)
	private TextView tv3;
	@ViewInject(value=R.id.tv4,onclick=false)
	private TextView tv4;
	@ViewInject(value=R.id.tv5,onclick=false)
	private TextView tv5;
	

	
	private Fragment fragment1;
	private Fragment fragment2;
	private Fragment fragment3;
	private Fragment fragment4;
	private Fragment fragment5;
	
	
	@Override
	protected void createActivity() {
		// TODO Auto-generated method stub
		
		setMenu(1);
		
	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.ll1:
			setMenu(1);
			break;
		case R.id.ll2:
			setMenu(2);
			break;
		case R.id.ll3:
			setMenu(3);
			break;
		case R.id.ll4:
			setMenu(4);
			break;
		case R.id.ll5:
			setMenu(5);
			break;
		}
	}


	private void setMenu(int pos){
		
		iv1.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon1));
		tv1.setTextColor(getResources().getColor(R.color.normal));
		iv2.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon2));
		tv2.setTextColor(getResources().getColor(R.color.normal));
		iv3.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon3));
		tv3.setTextColor(getResources().getColor(R.color.normal));
		iv4.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon4));
		tv4.setTextColor(getResources().getColor(R.color.normal));
		iv5.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon5));
		tv5.setTextColor(getResources().getColor(R.color.normal));
		
		
		 FragmentManager fm = this.getSupportFragmentManager();  
	        // 开启Fragment事务  
	     FragmentTransaction transaction = fm.beginTransaction();  
		
		if(pos==1){
			iv1.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon1_s));
			tv1.setTextColor(getResources().getColor(R.color.select));

			if(fragment1==null){
				fragment1=new IndexFragment();
			}
			transaction.replace(R.id.content, fragment1);
			
		}else if(pos==2){
			iv2.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon2_s));
			tv2.setTextColor(getResources().getColor(R.color.select));


		}else if(pos==3){
			iv3.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon3_s));
			tv3.setTextColor(getResources().getColor(R.color.select));

		}else if(pos==4){
			iv4.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon4_s));
			tv4.setTextColor(getResources().getColor(R.color.select));

		}else if(pos==5){
			iv5.setImageDrawable(getResources().getDrawable(R.drawable.tabbar_icon5_s));
			tv5.setTextColor(getResources().getColor(R.color.select));

		}
		
		transaction.commit();
	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			
			if ((System.currentTimeMillis() - ExitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出!", 3)
						.show();
				ExitTime = System.currentTimeMillis();
			} else {
				AppManager.INSTANCE.AppExit(this);
			}
			return true;
		}
		return true;
	}



}
